/**
 * Created by gim on 14-2-5.
 */
var assert = require("assert");
var mpl = require('../lib/model.js');
var path = require('path');
var db = require('./db/config.js').db;
var async = require('async');
var ids = [];

describe('model.js exports api :', function () {

    //set the User model to db
    before(function () {
        var User = require('./model/user.js');
        var count = 0;
        async.series([function (cb) {
            for (var i = 0; i < 10; i++) {
                var user = new User('user' + (1000 + i), 'pwd' + (1000 + i), 'init data');
                db.collection('user').save(user, null, function (err, user) {
                    ids.push(user._id);
                    cb(err, user);
                });
            }
        }], function (err, result) {
            if (err != null) {
                throw err;
            }
            count = count + 1;
            if (count == 10) {
                console.info(' init test data done !');
            }
        });

    });

    after(function () {
        console.info(' test finished ! ');
        db.collection('user').drop();
    });



    describe('methods : ', function () {
        before(function () {
            mpl.loadFolder(path.join(__dirname, 'model'), {db: db});
        });


        it('#save(callback)', function (done) {
            mpl.loadModel('user', {db: db});
            var user = new User();
            user.name = 'username';
            user.password = 'pwd23';
            async.series([function (cb) {
                user.save(function (err, user) {
                    cb(err, user);
                })
            }], function (err, result) {
                if (err != null) {
                    throw err;
                }
                User.findById(result[0]._id, function (err, user) {
                    assert.notEqual(user, null);
                    assert.equal(user.name, 'username');
                    assert.equal(user.password, 'pwd23');
                    done();
                })
            });

        });

        it('#findByOpt(opt,callback)', function (done) {
            mpl.loadModel('user', {db: db});
            User.findByOpt({name: 'user1001'}, function (err, users) {
                if (err != null) {
                    throw err;
                }
                assert.notEqual(users[0], null);
                assert.equal(users[0].name, 'user1001');
                assert.equal(users[0].password, 'pwd1001');
                done();
            });

        });

        it('#modify(id, option, callback)', function (done) {
            mpl.loadModel('user', {db: db});
            async.waterfall([function (cb) {
                User.findByOpt({name: 'user1001'}, function (err, users) {
                    cb(err, users);
                });
            }, function (n, cb) {
                User.modify(n[0]._id, {name: 'user3001'}, function (err, result) {
                    // number of updated records
                    cb(err, n[0]._id);
                });
            }, function (n, cb) {
                User.findById(n, function (err, user) {
                    assert.notEqual(user, null);
                    assert.equal(user.name, 'user3001');
                    cb(err, user);
                });
            }], function (err, result) {
                done();
            });

        });

        it('#list(callback)', function (done) {
            User.list(function (err, users) {
                assert.notEqual(users.length, 0);
            });
            done();
        });

        it('#findById(id, callback)', function (done) {
            User.findById(ids[0], function (err, u) {
                assert.equal(u.password, 'pwd1000');
                done();
            });
        });

        it('#delById(id, callback)', function (done) {
            User.delById(ids[0], function (err, result) {
                assert.equal(result, 1);
                done();
            });
        });

    });

});

